home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / COMMS / C101.ZIP / UUPC11XS.ZIP / MAIL / SYSALIAS.C < prev    next >
C/C++ Source or Header  |  1992-12-05  |  9KB  |  227 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    S y s A l i a s . C                                             */
  3. /*                                                                    */
  4. /*    System wide alias support for UUPC/extended                     */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Changes Copyright (c) 1990-1992 by Kendra Electronic            */
  9. /*    Wonderworks.                                                    */
  10. /*                                                                    */
  11. /*    All rights reserved except those explicitly granted by the      */
  12. /*    UUPC/extended license agreement.                                */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: SYSALIAS.C 1.2 1992/12/05 23:38:43 ahd Exp $
  21.  *
  22.  *    $Log: SYSALIAS.C $
  23.  * Revision 1.2  1992/12/05  23:38:43  ahd
  24.  * Skip blanks as well as unprintable characters
  25.  *
  26.  * Revision 1.1  1992/12/04  01:00:27  ahd
  27.  * Initial revision
  28.  *
  29.  */
  30.  
  31. /*--------------------------------------------------------------------*/
  32. /*                        System include files                        */
  33. /*--------------------------------------------------------------------*/
  34.  
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <time.h>
  39. #include <limits.h>
  40. #include <ctype.h>
  41.  
  42. /*--------------------------------------------------------------------*/
  43. /*                    UUPC/extended include files                     */
  44. /*--------------------------------------------------------------------*/
  45.  
  46. #include "lib.h"
  47. #include "deliver.h"
  48. #include "sysalias.h"
  49. #include "hlib.h"
  50.  
  51. /*--------------------------------------------------------------------*/
  52. /*                          Global variables                          */
  53. /*--------------------------------------------------------------------*/
  54.  
  55. char *SysAliases = NULL;      /* Name of our system alias file       */
  56. static ALIASTABLE *aliasTable = NULL;
  57. static int aliases = 0;
  58.  
  59. currentfile();
  60.  
  61. /*--------------------------------------------------------------------*/
  62. /*                             Prototypes                             */
  63. /*--------------------------------------------------------------------*/
  64.  
  65. static void InitAlias( void );
  66.  
  67. /*--------------------------------------------------------------------*/
  68. /*    c h e c k a l i a s                                             */
  69. /*                                                                    */
  70. /*    Check the system alias table for a user.  We perform a linear   */
  71. /*    search on the unordered table we we don't expect to perform     */
  72. /*    many searchs in one execution of the program, and this saves    */
  73. /*    sorting it.                                                     */
  74. /*--------------------------------------------------------------------*/
  75.  
  76. ALIASTABLE *checkalias( const char *user )
  77. {
  78.    int subscript = 0;
  79.  
  80.    if ( SysAliases == NULL )
  81.       InitAlias();
  82.  
  83.    for ( subscript = 0; subscript < aliases; subscript ++ )
  84.    {
  85.       if ( equali(aliasTable[subscript].alias , user ))
  86.          return &aliasTable[subscript];
  87.    } /* for */
  88.  
  89. /*--------------------------------------------------------------------*/
  90. /*                        No hit, return NULL                         */
  91. /*--------------------------------------------------------------------*/
  92.  
  93.    return NULL;
  94. } /* checkalias */
  95.  
  96. /*--------------------------------------------------------------------*/
  97. /*       I n i t A l i a s                                            */
  98. /*                                                                    */
  99. /*       Initialize our system alias table                            */
  100. /*--------------------------------------------------------------------*/
  101.  
  102. static void InitAlias( void )
  103. {
  104.    char buf[BUFSIZ];
  105.    int subscript  = -1;
  106.    int maxaliases = 64;
  107.    boolean inAlias = FALSE;
  108.    FILE *stream;
  109.    long here;
  110.  
  111. /*--------------------------------------------------------------------*/
  112. /*            Build the file name and try to open the file            */
  113. /*--------------------------------------------------------------------*/
  114.  
  115.    mkfilename( buf, E_confdir, "aliases" );
  116.    SysAliases = newstr( buf );
  117.  
  118.    stream = FOPEN( SysAliases , "r", TEXT );
  119.    if ( stream == NULL )
  120.    {
  121.       if (debuglevel > 1)
  122.          printerr( SysAliases );
  123.       return;
  124.    } /* if */
  125.  
  126. /*--------------------------------------------------------------------*/
  127. /*                 The file is open, allocate a table                 */
  128. /*--------------------------------------------------------------------*/
  129.  
  130.    aliasTable = malloc( sizeof *aliasTable * maxaliases );
  131.  
  132.    here = ftell( stream );       /* Remember location in file        */
  133.  
  134. /*--------------------------------------------------------------------*/
  135. /*                    Begin loop to process names                     */
  136. /*--------------------------------------------------------------------*/
  137.  
  138.    while (fgets( buf , BUFSIZ , stream ) != NULL )
  139.    {
  140.       char *s = buf;
  141.  
  142. /*--------------------------------------------------------------------*/
  143. /*    Ignore comments, lines that begin with whitespace, and empty    */
  144. /*    lines                                                           */
  145. /*--------------------------------------------------------------------*/
  146.  
  147.       while( *s && ! isgraph( *s ))
  148.          s++;
  149.  
  150.       if (*s == '#')
  151.          continue;
  152.  
  153.       if ( ! *s )                /* Empty line?                      */
  154.       {                          /* Yes --> 'tis end of alias        */
  155.          if ( inAlias )
  156.          {
  157.             inAlias = FALSE;
  158.             if (aliasTable[subscript].start == -1 )
  159.                printmsg(0,"%s: Invalid alias %s, no data defined!",
  160.                   SysAliases, aliasTable[subscript].alias );
  161.             else
  162.                aliasTable[subscript].end = here;
  163.          }
  164.       } /* if ( ! *s ) */
  165.       else if (inAlias)
  166.       {
  167.          if (aliasTable[subscript].start == -1 )
  168.             aliasTable[subscript].start = here + (s - buf);
  169.       }
  170.       else {                           /* Start of a new alias */
  171.          char *colon = strchr( s, ':' );
  172.  
  173.          if (s != buf )
  174.             printmsg(0,"%s: Error aliases must begin in column 1",
  175.                   SysAliases );
  176.  
  177.          if (colon == NULL )
  178.          {
  179.             printmsg(0,"%s: No colon after alias %s",
  180.                   SysAliases, s);
  181.             continue;
  182.          }
  183.  
  184.          if ( subscript+2 == maxaliases )
  185.          {
  186.             maxaliases *= 2;
  187.             aliasTable = realloc( aliasTable,
  188.                                   maxaliases * sizeof *aliasTable );
  189.             checkref( aliasTable );
  190.          }
  191.  
  192.          *colon = '\0';          /* Terminate the name               */
  193.          aliasTable[++subscript].alias = newstr(strtok(s,WHITESPACE));
  194.                                  /* Save name of alias               */
  195.          s = strtok( colon+1, WHITESPACE);
  196.                                  /* Find next token                  */
  197.  
  198.          if ((s == NULL) || (*s == '#'))  /* Any alias on same?      */
  199.              aliasTable[subscript].start = -1;
  200.                                  /* No--> Look for it later          */
  201.          else
  202.              aliasTable[subscript].start = here + ( s - buf );
  203.  
  204.           aliasTable[subscript].end  = -1;
  205.           inAlias = TRUE;
  206.       } /* if */
  207.  
  208.       here = ftell( stream );       /* Remember start of next line   */
  209.  
  210.    } /* while */
  211.  
  212. /*--------------------------------------------------------------------*/
  213. /*                   Clean up and return to caller                    */
  214. /*--------------------------------------------------------------------*/
  215.  
  216.    if ( inAlias )
  217.       aliasTable[subscript].end = LONG_MAX;
  218.  
  219.    aliases = subscript + 1;
  220.    if ( aliases == 0 )
  221.       free( aliasTable );
  222.    else
  223.       aliasTable = realloc( aliasTable, aliases * sizeof *aliasTable );
  224.    fclose( stream );
  225.  
  226. } /* InitAlias */
  227.